<?php
include "include/config.inc.php";
if(!isset($_SESSION['s_activId']))
{
  header('location:index.php');
}

$payment = isset($_REQUEST['payment']) ? $_REQUEST['payment'] : 0;
if(isset($_POST['submitBtn']))
{
  $transDate   = $_POST['transDateYear']."-".$_POST['transDateMonth']."-".$_POST['transDateDay'];
  $voucherType = isset($_POST['voucherType']) && ($_POST['voucherType'] != '') ? $_POST['voucherType'] : '';
  $voucherNo   = isset($_POST['voucherNo']) && ($_POST['voucherNo'] != '') ? $_POST['voucherNo'] : '';
  
  $insertMaster = "INSERT INTO transmaster (voucherType,transDate,voucherNo)
                        VALUES ('".$voucherType."','".$transDate."','".$voucherNo."')";
  $insertMasterRes = mysql_query($insertMaster);
  $lastMasterId = mysql_insert_id();
  if(!$insertMasterRes)
  {
  	echo "Insert Query Master Not Inserted".mysql_error();
  }
  else
  {
    if($_POST['voucherType'] == 'Receipt')
    {
    	$loopCountDr = 0;
    	while($loopCountDr < count ($_POST['drAccountId']))
    	{
    		$drAccountId    = ($_POST['drAccountId'][$loopCountDr] != '') ? $_POST['drAccountId'][$loopCountDr] : 0;
    		$loanId         = ($_POST['loanId'][$loopCountDr] != '') ? $_POST['loanId'][$loopCountDr] : 0;
    		$loanOrInterest = ($_POST['loanOrInterest'][$loopCountDr] != '') ? $_POST['loanOrInterest'][$loopCountDr] : 0;
    		$drAmount       = ($_POST['drAmount'][$loopCountDr] != '') ? $_POST['drAmount'][$loopCountDr] : 0;
    		if($_POST['drAmount'][$loopCountDr] != '' && $_POST['drAmount'][$loopCountDr] > 0)
    		{
    			$insertDrDetail = "INSERT INTO transdetail (transMasterId,drAccountId,loanId,loanOrInterest,drAmount)
  			                          VALUES (".$lastMasterId.",".$drAccountId.",".$loanId.",'".$loanOrInterest."',".$drAmount.")";
  			  $insertDrDetailRes = mysql_query($insertDrDetail);
    		}
    		$loopCountDr++;
    	}
      $crAccountId = isset($_POST['crAccountId']) && ($_POST['crAccountId'] > 0) ? $_POST['crAccountId'] : 0;
      $crAmount    = isset($_POST['crAmount']) && ($_POST['crAmount'] != '') ? $_POST['crAmount'] : 0;
      $insertCrDetail = "INSERT INTO transdetail (transMasterId,crAccountId,crAmount)
                              VALUES (".$lastMasterId.",".$crAccountId.",".$crAmount.")";
      $insertCrDetailRes = mysql_query($insertCrDetail);
    }
    elseif($_POST['voucherType'] == 'Payment')
    {
    	$loopCountDr = 0;
    	while($loopCountDr < count ($_POST['drAccountId']))
    	{
    		$drAccountId    = ($_POST['drAccountId'][$loopCountDr] != '') ? $_POST['drAccountId'][$loopCountDr] : 0;
    		$loanId         = ($_POST['loanId'][$loopCountDr] != '') ? $_POST['loanId'][$loopCountDr] : 0;
    		$loanOrInterest = ($_POST['loanOrInterest'][$loopCountDr] != '') ? $_POST['loanOrInterest'][$loopCountDr] : 0;
    		$drAmount       = ($_POST['drAmount'][$loopCountDr] != '') ? $_POST['drAmount'][$loopCountDr] : 0;
    		if($_POST['drAmount'][$loopCountDr] != '' && $_POST['drAmount'][$loopCountDr] > 0)
    		{
    			$insertDrDetail = "INSERT INTO transdetail (transMasterId,crAccountId,loanId,loanOrInterest,crAmount)
  			                          VALUES (".$lastMasterId.",".$drAccountId.",".$loanId.",'".$loanOrInterest."',".$drAmount.")";
  			  $insertDrDetailRes = mysql_query($insertDrDetail);
    		}
    		$loopCountDr++;
    	}
    	$crAccountId = isset($_POST['crAccountId']) && ($_POST['crAccountId'] > 0) ? $_POST['crAccountId'] : 0;
      $crAmount    = isset($_POST['crAmount']) && ($_POST['crAmount'] != '') ? $_POST['crAmount'] : 0;
      $insertCrDetail = "INSERT INTO transdetail (transMasterId,drAccountId,drAmount)
                              VALUES (".$lastMasterId.",".$crAccountId.",".$crAmount.")";
      $insertCrDetailRes = mysql_query($insertCrDetail);
    }
  	header("Location:transaction.php");
  }
  
}

//select Main Account : Start
$selectMainAccount = "SELECT accountId,accountName
                        FROM account
                       WHERE isDayBook = 1
                    ORDER BY accountName";
$selectMainAccountRes = mysql_query($selectMainAccount);
$mainAccArray = array();
$m = 0;
while($mainAccRow = mysql_fetch_array($selectMainAccountRes))
{
  $mainAccArray['accountId'][$m]   = $mainAccRow['accountId'];
  $mainAccArray['accountName'][$m] = $mainAccRow['accountName'];
  $m++;
}
//select Main Account : End

//select Account : Start
$selectAccount = "SELECT accountId,accountName
                    FROM account
                   WHERE isDayBook != 1
                ORDER BY accountName";
$selectAccountRes = mysql_query($selectAccount);
$accArray = array();
$a = 0;
while($accRow = mysql_fetch_array($selectAccountRes))
{
  $accArray['accountId'][$a]   = $accRow['accountId'];
  $accArray['accountName'][$a] = $accRow['accountName'];
  $a++;
}
//select Account : End

////Dr CR Array for Item: Start
$paymentValuesArr[0] = "Payment";
$paymentOutputArr[0] = "Payment";
$paymentValuesArr[1] = "Receipt";
$paymentOutputArr[1] = "Receipt";
$paymentValuesArr[2] = "Journal";
$paymentOutputArr[2] = "Journal";
////Dr CR Array for Item: End
$loanInterestArray[0] = "Loan Amount";
$loanInterestArray[1] = "Loan Interest";
include("bottom.php");
$smarty->assign('paymentValuesArr',$paymentValuesArr);
$smarty->assign('paymentOutputArr',$paymentOutputArr);
$smarty->assign('mainAccArray',$mainAccArray);
$smarty->assign('accArray',$accArray);
$smarty->assign('payment',$payment);
$smarty->assign('loanInterestArray',$loanInterestArray);
$smarty->display('transaction.tpl');  
?>